package com.fduss.artStore.dao.impl;

import com.fduss.artStore.dao.SubjectDao;
import com.fduss.artStore.entity.Subject;
import com.fduss.artStore.util.JdbcUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by alex on 28/06/2017.
 */
public class SubjectDaoImpl implements SubjectDao {
    private JdbcUtil util;

    public SubjectDaoImpl() {
        util = new JdbcUtil();
    }

    @Override
    public Subject add(Subject subject) {
        Connection conn = util.getConnection();
        String sql = "INSERT INTO `art`.`subject` (`SubjectId`, `SubjectName`) VALUES (?, ?)";
        PreparedStatement pst = null;
        boolean flag = false;

        try{
            pst = conn.prepareStatement(sql);
            pst.setInt(1, subject.getSubjectId());
            pst.setString(2, subject.getSubjectName());
            pst.executeUpdate();
            flag = true;
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            util.close(null, pst, conn);
        }
        return flag ? subject : null;
    }

    @Override
    public void delete(int subjectId) {
        Connection conn = util.getConnection();
        String sql = "DELETE FROM `art`.`subject` WHERE `SubjectId` = ?";
        PreparedStatement pst = null;

        try{
            pst = conn.prepareStatement(sql);
            pst.setInt(1, subjectId);
            pst.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            util.close(null, pst, conn);
        }
    }

    @Override
    public Subject getByArtwork(int artworkId) {
        //todo
        return null;
    }

    @Override
    public Subject getOne(int subjectId) {
        Connection conn = util.getConnection();
        String sql = "SELECT * FROM `art`.`subject` WHERE `SubjectId` = ?";
        PreparedStatement pst = null;
        ResultSet rs = null;
        Subject subject = null;

        try{
            pst = conn.prepareStatement(sql);
            pst.setInt(1, subjectId);
            rs = pst.executeQuery();

            if (rs.next()){
                subject = buildSubject(rs);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            util.close(rs, pst, conn);
        }
        return subject;
    }

    @Override
    public List<Subject> getAll() {
        Connection conn = util.getConnection();
        String sql = "SELECT * FROM `art`.`subject`";
        PreparedStatement pst = null;
        ResultSet rs = null;
        List<Subject> subject = new ArrayList<>();
        try{
            pst = conn.prepareStatement(sql);
            rs = pst.executeQuery();
            while (rs.next()){
                subject.add(buildSubject(rs));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            util.close(rs, pst, conn);
        }

        return subject;
    }

    private Subject buildSubject(ResultSet rs) throws SQLException {
        Subject subject = new Subject();
        subject.setSubjectId(rs.getInt("SubjectId"));
        subject.setSubjectName(rs.getString("SubjectName"));
        return subject;
    }
}
